<template>
    <div>
        <!-- 下拉菜单类型row-key="id" -->
        <el-table :data="PermisstionArr" row-key="id" border style="width: 100%; margin-bottom: 20px">
            <el-table-column label="名称" prop="name">
            </el-table-column>
            <el-table-column label="权限值" prop="code">
            </el-table-column>
            <el-table-column label="修改时间" prop="updateTime">
            </el-table-column>
            <el-table-column label="操作">
                <template #="{ row, $index }">
                    <el-button type="primary" size="small" @click="addPermisstion(row)"
                        :disabled="row.level == 4 ? true : false">{{
                            row.level == 3 ? '添加功能' : '添加菜单' }}</el-button>
                    <el-button type="primary" size="small" @click="updatePermisstion(row)"
                        :disabled="row.level > 1 ? false : true">编辑</el-button>
                    <el-popconfirm :title="`你确定删除${row.name}吗？`" @confirm="removeMenu(row.id)">
                        <template #reference>
                            <el-button type="primary" size="small"
                                :disabled="row.level > 1 ? false : true">删除</el-button>
                        </template>
                    </el-popconfirm>

                </template>
            </el-table-column>
        </el-table>
        <!-- 对话框 -->
        <el-dialog v-model="dialogFormVisible" :title="menuData.id ? '编辑菜单' : '添加菜单'" width="800px">
            <el-form :model="menuData">
                <el-form-item label="名称">
                    <el-input placeholder="请你输入菜单名称" v-model="menuData.name" prop="name"></el-input>
                </el-form-item>
                <el-form-item label="权限">
                    <el-input placeholder="请你输入权限数值" v-model="menuData.code" prop="code"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="save">确定</el-button>
            </template>
        </el-dialog>

    </div>
</template>

<script setup lang="ts">
import { reqAddOrUpdateMenu, reqAllPermisstion, reqRemoveMenu } from '@/api/acl/menu';
import { PermisstionList, MenuParams } from '@/api/acl/menu/type';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';
// 全部菜单数据
let PermisstionArr = ref<PermisstionList>([]);
// 控制对话框
let dialogFormVisible = ref<boolean>(false);
// 收集添加的数据
let menuData = reactive<MenuParams>({
    code: '',
    level: 0,
    name: '',
    pid: 0.,//上一级的level
})
onMounted(() => {
    getHasPermisstion()
})
// 获取菜单数据
const getHasPermisstion = async () => {
    let result: any = await reqAllPermisstion();
    if (result.code == 200) {
        PermisstionArr.value = result.data
    }


}
// 添加菜单按钮的回调函数
const addPermisstion = (row: any) => {
    // 清空数据
    Object.assign(menuData, {
        id: 0,
        code: '',
        level: 0,
        name: '',
        pid: 0.,//上一级的level
    })
    // 打开对话框
    dialogFormVisible.value = true;
    // 收集数据
    menuData.level = row.level + 1;
    menuData.pid = row.id;

}
// 对话框确定按钮的回调函数
const save = async () => {

    // 发生请求
    let result: any = await reqAddOrUpdateMenu(menuData);
    if (result.code == 200) {
        // 关闭对话框
        dialogFormVisible.value = false;
        ElMessage({ type: 'success', message: menuData.id ? '更新成功' : '添加成功' });
        // 重新获取数据
        getHasPermisstion();
    }

}
// 编辑按钮的回调函数
const updatePermisstion = (row: any) => {
    // 获取数据
    Object.assign(menuData, row);
    dialogFormVisible.value = true
}
// 删除按钮的回调函数
const removeMenu = async (id: number) => {
    let result: any = await reqRemoveMenu(id);
    if (result.code == 200) {
        ElMessage({ type: 'success', message: '删除成功' });
        // 重新获取数据
        getHasPermisstion()
    }
}
</script>

<style scoped></style>